package camid.util.dataanalysis;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map.Entry;

public class DataAnalysisScript {

	public static File getLastExperimentDir(String exp)
	{
		File parentDir = new File("/home/diego/development/workspace-middleware/camid3/results/aws-" + exp);
		long srctime = 0;
		File expDir = null;
		
		for(File file : parentDir.listFiles())
		{
			long time = file.lastModified();
			if(time >= srctime)
			{
				srctime = time;
				expDir = file;
			}
		}
		return expDir;
	}
	
	public static void main(String[] args) throws IOException 
	{
		String[] experiments = new String[]{"migration","nomigration","replication","noreplication"};
		File statsDir = new File("statistics");
		statsDir.mkdirs();
		FileOutputStream fos = null;
		FileInputStream fis = null;
		for(String exp : experiments)
		{	
			File dir = getLastExperimentDir(exp);
			dir.length();
			
			DowntimeLogAnalyser analyser = new DowntimeLogAnalyser();
	
			fis = new FileInputStream(new File(dir,"jmeter-sdout.log"));
			analyser.analyseStream(fis);
			fis.close();
			
			fos = new FileOutputStream(new File(statsDir,"downtime-" + exp + ".csv"));
			analyser.toStream(fos);
			fos.close();
			
			fos = new FileOutputStream(new File(statsDir,"downtime-" + exp + "-avgs.csv"));
			analyser.avgsToStream(fos);
			fos.close();
			
			
			fis = new FileInputStream(new File(dir,"expMiddleware.log"));
			
			JMeterAnalyser jmanalyser = new JMeterAnalyser();
			jmanalyser.analyseStream(fis);
			
			fis.close();
			
			
			fos = new FileOutputStream(new File(statsDir,String.format("timestamp-%s.csv", exp)));
			for(Entry<JMeterTG, JMeterMetricSet> entry : jmanalyser.getMetricSets().entrySet())
			{
				JMeterMetricSet metricSet = entry.getValue();
				JMeterTimestampAnalyser tstampAnalyser = new JMeterTimestampAnalyser(metricSet);
				tstampAnalyser.analyse();
				tstampAnalyser.toStream(fos);
			}
			fos.close();
			
			
			fos = new FileOutputStream(new File(statsDir,"responsetime-"+ exp + ".csv"));
			jmanalyser.toStream(fos);
			fos.close();
			
		}
	}
	
}
